package com.example.lib_log

import com.example.lib_log.logger.EmailLogger
import com.example.lib_log.logger.FileLogger
import com.example.lib_log.logger.ILogger
import com.example.lib_log.logger.LogCatLogger
import com.example.lib_log.logger.NetLogger

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/22
 */
class LoggerManager private constructor(
    private val TAG:String,
    private val level:LoggerLevel,
    private val loggerStrategy: LoggerStrategy,
    private val isDebug:Boolean,
    private val saveUrl:String
){
    //**********策略者模式*******************
    //3:根据不同的策略创建不同的logger
    private var logger:ILogger = when(loggerStrategy){
        LoggerStrategy.LOGCAT -> LogCatLogger()
        LoggerStrategy.FILE -> FileLogger()
        LoggerStrategy.EMAIL -> EmailLogger()
        LoggerStrategy.NET -> NetLogger()
    }
    //4:设置属性
    init {
        logger.setLoggerLevel(level)
        logger.setLogTAG(TAG)
        logger.setDebug(isDebug)
        logger.setSaveUrl(saveUrl)
    }

    //4:提供打印日志方法
    fun v(Tag:String,log:String){
        logger.v(Tag,log)
    }
    fun d(Tag:String,log:String){
        logger.d(Tag,log)
    }
    fun i(Tag:String,log:String){
        logger.i(Tag,log)
    }
    fun w(Tag:String,log:String){
        logger.w(Tag,log)
    }
    fun e(Tag:String,log:String){
        logger.e(Tag,log)
    }






    //2:次构造，Builder作为参数
    constructor(builder: Builder):this(
        builder.TAG,
        builder.level,
        builder.loggerStrategy,
        builder.isDebug,
        builder.saveUrl
    )

    //**********构建者模式*******************
    //1:定义构建类
    class Builder{
         var TAG:String = "默认TAG"
         var level:LoggerLevel = LoggerLevel.Verbose
         var loggerStrategy: LoggerStrategy = LoggerStrategy.LOGCAT
         var isDebug:Boolean = true
         var saveUrl:String = ""

        fun setTAG(TAG:String):Builder{
            this.TAG = TAG
            return this //链式调用
        }
        fun setLoggerLevel(level:LoggerLevel):Builder{
            this.level = level
            return this
        }
        fun setLoggerStrategy(loggerStrategy: LoggerStrategy):Builder{
            this.loggerStrategy = loggerStrategy
            return this
        }
        fun setDebug(isDebug:Boolean):Builder{
            this.isDebug = isDebug
            return this
        }
        fun setSaveUrl(url:String):Builder{
            this.saveUrl = url
            return this
        }

        /**
         * 构建对象
         */
        fun build():LoggerManager{
            return LoggerManager(this)
        }



    }


}